/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package eliminar;

import apunte.Carrera;
import apunte.ManejoApunte;
import crear.crearCarrera;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

/**
 *
 * @author martinLequerica
 */
public class eliminarCarrera {

    private LinkedList<Carrera> llc;
    private java.awt.List list1;
    private javax.swing.JScrollPane jScrollPane1;
    private VistaEliminar ve;
    private java.awt.List lista;
    private JButton botonMas, botonMenos, refresh;
    private ManejoApunte ma;

    public eliminarCarrera() throws SQLException {
        ve = new VistaEliminar();
        Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
        ve.setLocation(d.width/2,d.height/2);
        ma = new ManejoApunte();
        botonMas = ve.getBotonMas();
        botonMenos = ve.getBotonMenos();
        refresh = ve.getRefresh();







        //varibales
        lista = ve.getLista();
        llc = new LinkedList();

        cargarLinkedList();
        cargarLista(llc);





        ve.setVisible(true);


        botonMas.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {

                new crearCarrera();
                try {
                    cargarLinkedList();
                } catch (SQLException ex) {
                    Logger.getLogger(eliminarCarrera.class.getName()).log(Level.SEVERE, null, ex);
                }


            }
        });

        refresh.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {

                llc = new LinkedList();
                try {
                    cargarLinkedList();
                } catch (SQLException ex) {
                    Logger.getLogger(eliminarCarrera.class.getName()).log(Level.SEVERE, null, ex);
                }
                cargarLista(llc);
            }

            

            

            

            
        });

        botonMenos.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
                try {
                    verificoDependencias(llc.get(lista.getSelectedIndex()));
                    
                } catch (SQLException ex) {
                    Logger.getLogger(eliminarCarrera.class.getName()).log(Level.SEVERE, null, ex);
                }


            }
        });



    }
    
    private void verificoDependencias(Carrera ca) throws SQLException{
        boolean verDep = ma.verificoDependenciasCarrera(ca);
        
        if (verDep == false){
            
            
        
            
            ma.eliminarCarrera(ca);
            cargarLinkedList();
            cargarLista(llc);
            JOptionPane.showMessageDialog(null, "La carrera fue eliminada");
            
        }
        
        
        
        
        
    }

    private void cargarLinkedList() throws SQLException {
        llc.clear();
        llc = ma.traerCarreras();
    }

    public void cargarLista(LinkedList llc) {

        lista.removeAll();
        Iterator<Carrera> i = llc.iterator();
        while (i.hasNext()) {
            Carrera next = i.next();
            lista.add(next.getNombreCarrera());


        }
    
    
    
}
}
